package top.starmc.starquesthelper.util

import eos.moe.dragoncore.network.PacketSender
import ink.ptms.chemdah.api.ChemdahAPI.chemdahProfile
import ink.ptms.chemdah.api.ChemdahAPI.isChemdahProfileLoaded
import org.bukkit.entity.Player
import taboolib.platform.compat.replacePlaceholder
import top.starmc.starquesthelper.config.Config

fun Player.syncQuest() {
    if (isOnline && isChemdahProfileLoaded) {
        val placeholder = LinkedHashMap<String, String>()
        var amount = 0
        chemdahProfile.getQuests().forEach {
            Config.questMap[it.id]?.let { data ->
                placeholder["Quest_X_$amount"] = data.x.toString()
                placeholder["Quest_Y_$amount"] = data.y.toString()
                placeholder["Quest_Z_$amount"] = data.z.toString()
                placeholder["Quest_Name_$amount"] = data.name
                placeholder["Quest_World_$amount"] = data.world
                placeholder["Quest_Desc_$amount"] = data.desc.replacePlaceholder(this)
                amount++
            }
        }
        placeholder["Quest_Amount"] = amount.toString()
        PacketSender.sendSyncPlaceholder(this, placeholder)
    }
}